package gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Logger;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.border.TitledBorder;

import domein.PeerController;
import domein.models.BestandenListModel;
import domein.models.PeersListModel;

import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class BestandenPanel extends JPanel 
{	
	public static Logger logger = Logger.getLogger("MyLog");
	private static final long serialVersionUID = 1L;
	private BestandenListModel bestandenListModel;
	private PeersListModel peersListModel;
	private JTable beschikbareBestandenTable;
	private JTable beschikbarePeersTable;

	public BestandenPanel() 
	{
		//bestanden opvragen van de peer
		this.bestandenListModel = PeerController.getInstance().getBestandenListModel();
		this.peersListModel = PeerController.getInstance().getPeerListModel();
		
		initGui();
	}
	
	public void initGui()
	{
		//default borderlayout uitzetten
		setLayout(null);
		
		beschikbarePeersTable = new JTable(peersListModel);
		beschikbarePeersTable.setVisible(true);
		beschikbarePeersTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION );
		
		JScrollPane peersScrollPane = new JScrollPane(beschikbarePeersTable);
		peersScrollPane.setBorder(new TitledBorder(null, "Beschikbare peers", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		peersScrollPane.setBounds(0,0,395,500);	//absolute positionering
		
		beschikbareBestandenTable = new JTable(bestandenListModel);
		beschikbareBestandenTable.setVisible(true);
		beschikbareBestandenTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION );

		JScrollPane bestandenScrollPane = new JScrollPane(beschikbareBestandenTable);
		bestandenScrollPane.setBorder(new TitledBorder(null, "Beschikbare bestanden", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		bestandenScrollPane.setBounds(400,0,395,500); //Abslolute positionering
			
		add(peersScrollPane);
		add(bestandenScrollPane);
		
		JButton btnDownload = new JButton("Download");
		btnDownload.setBounds(650,502,100,25);
		btnDownload.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0) 
			{
				try
				{
					//Download file
					GuiController.getInstance().getPeerController().downloadPeerFile(bestandenListModel.getPeerFileForIndex(beschikbareBestandenTable.getSelectedRow()));
				}
				catch(IndexOutOfBoundsException e)
				{
					logger.info("User clicked 'Download-button', but no file was selected.");
				}
				catch(NullPointerException e)
				{
					e.printStackTrace();
				}
			}
		});
		
		beschikbarePeersTable.getSelectionModel().addListSelectionListener(new ListSelectionListener()
		{
			@Override
			public void valueChanged(ListSelectionEvent arg0) 
			{
				try
				{
					//Show files of peer
					if (beschikbarePeersTable.getSelectedRow() != -1) 
					{
						GuiController.getInstance().getPeerController().getBestandenListModel().setSelectedPeer(peersListModel.getPeerForIndex(beschikbarePeersTable.getSelectedRow()));
					}
				}
				catch(NullPointerException e)
				{
					e.printStackTrace();
				}
			}
		});
		
		add(btnDownload);
	}
}
